/**
 * 分组管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="margin-t10">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>分组管理</el-breadcrumb-item>
    </el-breadcrumb>

    <div class="boxBlock">
      <div class="serachTop">  
        <!-- 搜索筛选 -->
        <el-form :inline="true" :model="formInline" class="user-search">
          <el-form-item >
              <el-input
                size="small"
                v-model="formInline.groupName"
                placeholder="请输入分组名称" 
                clearable
              ></el-input>
          </el-form-item>  
          <!-- <el-form-item label="">
            <el-select
              size="small"
              v-model="formInline.status" 
              placeholder="状态"
              clearable
            > 
              <el-option 
                label="正常"
                :value="0" 
              ></el-option>
              <el-option 
                label="停用" 
                :value="1" 
              ></el-option>
            </el-select>
          </el-form-item>  -->
          <!-- <el-form-item label=""> 
            <el-select
              size="small"
              v-model="formInline.type" 
              placeholder="请选择卡状态"
              clearable
            > 
              <el-option 
                label="星链卡"
                :value="0" 
              ></el-option>
              <el-option 
                label="星汇卡" 
                :value="1" 
              ></el-option>
              <el-option 
                label="星际卡" 
                :value="2" 
              ></el-option>
            </el-select> 
          </el-form-item>  -->
          <el-form-item>
            <el-button
              size="small"
              type="primary"
              icon="el-icon-search"
              @click="search"
            >
              搜索
            </el-button>
            <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              @click="handleEdit()" 
            >
              新增
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <!--列表-->
    <div class="ra_box">      
      <el-table
        size="small"
        :data="listData"
        highlight-current-row
        v-loading="loading"
        element-loading-text="拼命加载中"
        style="width: 100%"
        v-if="listData"
        @selection-change="toggleSelection"
      >
        <!-- <el-table-column type="selection" width="55"> </el-table-column> -->
        <!-- <el-table-column
             prop="userId"
            label="用户ID"
            show-overflow-tooltip
            width="120"
          > -->
        <!-- </el-table-column> -->
        <el-table-column
            prop="groupName"
            label="分组名称"
            show-overflow-tooltip
            width="280"
        > </el-table-column>
        <!-- <el-table-column
            prop="status"
            label="状态"
            show-overflow-tooltip
            width="120"
        > 
           <template slot-scope="scope">
              <el-tag type="success" plain v-if="scope.row.status==0"> 正常</el-tag> 
              <el-tag type="info" plain v-else>停用</el-tag>
            </template>  
        </el-table-column> -->

        <el-table-column
            prop="type"
            label="卡类型"
            show-overflow-tooltip
            width="260"
        > 
            <template slot-scope="scope">
              {{  scope.row.type==0?'星链卡':scope.row.type==1?'星汇卡':scope.row.type==2?'星际卡':'-' }}
            </template>
        </el-table-column>   
        <el-table-column
            prop="updateTime"
            label="更新时间"
            show-overflow-tooltip 
            width="260"
        > 
            <template slot-scope="scope">
              {{ timestampToTime(scope.row.updateTime) }}
            </template>
        </el-table-column>   
        
        <el-table-column
            prop="createTime"
            label="创建时间"
            show-overflow-tooltip
            width="190"
        > 
            <template slot-scope="scope">
              {{ timestampToTime(scope.row.createTime) }}
            </template>
        </el-table-column> 

       
        <el-table-column align="center" label="操作" >
          <template slot-scope="scope">
            <el-button
               size="mini"  type="success" plain
              @click="handleEdit(scope.row)" :key="scope.$index" 
            >
              修改
            </el-button>
            <el-button
                size="mini"
                type="primary"
                @click="clickDel(scope.$index, scope.row)"
            >
                删除
            </el-button>
             
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination> 
    </div>
    

    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="35%"
      @close="closeDialog"
      class="editBox"
      :close-on-click-modal="false"
    >
      <el-form
        label-width="120px"
        :model="editForm"
        :rules="rules"
        ref="editForm" label-suffix="" 
      >        
        <el-form-item label="分组名称" prop="groupName" >
          <el-input
            size="small"
            v-model="editForm.groupName"
            auto-complete="off"
            placeholder="请输入分组名称"
          ></el-input>
        </el-form-item> 

        <!-- <el-form-item label="卡类型" prop="type" >  
          <el-select
            size="small"
            v-model="formInline.type" 
            placeholder="请选择卡状态"
            clearable
          > 
            <el-option 
              label="星链卡"
              :value="0" 
            ></el-option>
            <el-option 
              label="星汇卡" 
              :value="1" 
            ></el-option>
            <el-option 
              label="星际卡" 
              :value="2" 
            ></el-option>
          </el-select> 
        </el-form-item>  -->
        <!-- 0=星链,1=星汇,2=星际  --> 
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import {
  setStoreS,
  getStoreS,
  removeStoreS,
  insertCharacter,
  timestampToTime,
  timestampToData,
  phoneReg,
  emailReg,
  spStr, 
} from "../../utils/util";
import {
  phoneInfo,
  nation,
  getPhone,
  getNation,
  USzhou,
} from "../../utils/cardData";
import { baseURL, service, format, getUrl,reqJson } from "../../api/axiosFun";
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      loading: false,
      addFormVisible: false,
      editFormVisible: false,
      b: format,
      timestampToTime: timestampToTime,
      p: getPhone,
      n: getNation,
      dentialState: null,
      phoneInfo: phoneInfo,
      nation: nation,
      title: '添加',
      editForm: {
        userId:''
      },
      // rules表单验证
      rules: { 
        groupName: [
          { required: true, message: "请输入分组名称", trigger: "blur" },
        ],
        
      },
      formInline: {
        pageNum: 1,
        pageSize: 10,
        lastName: "",
        status: "",
        type: 1,
      },
      // 删除
      seletedata: {
        ids: "",
        token: localStorage.getItem("logintoken"),
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
      changeIDList: [], 
      cardType:1 
    };
  },
  // 注册组件
  components: {
    Pagination,
  },

  computed: {
    ...mapGetters(["getUser"]), 
  },
  watch: {
    getUser(newVal) {
      this.userInfo = JSON.parse(newVal);
      
    },
  }, 
  created() { 
    if(this.$store.state.user){
      this.userInfo = JSON.parse(this.$store.state.user);
     
    }
    this.getdata(this.formInline);
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    hasPermissionMethod(data) { 
      this.userInfo = JSON.parse(this.$store.state.user);
      return this.$hasPermission(this.userInfo,data) 
    },
    getdata(parameter) {
      let url = getUrl({
        ...parameter,
        type:1
      });  
      service
        .get("/api/photon/groupList" + url, {})
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.listData = res.rows;
            this.pageparm.currentPage = this.formInline.pageNum;
            this.pageparm.pageSize = this.formInline.pageSize;
            this.pageparm.total = res.total;
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch((err) => {
          this.$message.error(spStr(err, "PhotonPay", ""));
        });
    },
    getChild(e) {
      this.dentialState = e.child;
      this.editForm.residentialState = "";
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.pageNum = parm.currentPage;
      this.formInline.pageSize = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() {  
      if(this.userInfo){
        this.formInline.matrixAccount=this.cardType==1?'':this.userInfo.matrixAccount; 
      }
      // console.log(this.formInline,'this.formInline');
      this.getdata(this.formInline);
    },
    toggleSelection(rows) {
      this.changeIDList = [];
      rows.forEach((i) => {
        this.changeIDList.push(i.id);
      });
    }, 
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {     
          console.log(this.userInfo,'this.userInfo');
          this.editForm.userId = this.userInfo.userId; 
          this.editForm.parentUserId = this.userInfo.userId;   
          this.editForm.type = 1;   //0=星链,1=星汇,2=星际 
          service
            .post('/api/photon/editCardGroup', this.editForm)
            .then((res) => {
              this.loading = false;
              if (res.code == 0) {
                this.getdata(this.formInline);
                this.closeDialog();
                this.editForm = {};
              } else {
                this.$message({
                  type: "info",
                  message: spStr(res.msg, "PhotonPay", ""),
                });
              }
            })
            .catch((err) => {
              this.$message.error(spStr(err, "PhotonPay", ""));
            });
        } else {
          return false;
        }
      });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
      this.$refs.editForm.resetFields();
    },
    handleEdit(item){ 
        this.editFormVisible = true;
        console.log(item,'item')
        this.title= item&&item.groupId?"编辑":'添加'
        this.editForm = item?item:{
        };
    },
    clickDel(index,item){
      this.$confirm("确认删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(() => {
        this.loading = true;  
        reqJson('post',"/api/photon/removeCardGroup/"+item.groupId) 
        .then((res) => { 
          this.loading = false;
          if (res.code == 0) {
            this.getdata(this.formInline); 
            this.$message({
              type: "success",
              message: res.msg,
            });
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });


      })
    }
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.editBox .el-form-item {
  /* display: inline-block; */
}
.editBox .select10 .el-select,
.editBox .el-date-editor {
  width: 100%;
}
.editBox .mobilePrefix {
  width: 110px;
}
.userRole {
  width: 100%;
}
</style>

 
 